<?php

// +----------------------------------------------------------------------
// | GuoRan100 [ 果然100 你的选择 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州鲜生活电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.guoran100.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Org\BMap;

/**
 * 后台地图控制器
 * @author 曹梦龙 <138888611@qq.com>
 */
class LbsProjectAreaController extends AdminController {

    public function Area($name = "", $phone = "", $warehouse = "", $points = "",$pid='',$aid='',$areainfoid='',$mid='') {
        if (IS_POST) {
            $areaname=M('lbs_project_areainfo')->where(array('id'=>$areainfoid))->getField("name");
            $AreaData = array(
                'areaname' => $areaname, 
                'areainfoid' => $areainfoid, 
                'name' => $name,
                'pid'=>$pid,
                'aid'=>$aid,
                'mid'=>$mid,
                'phone' => $phone,
                'warehouse' => $warehouse,
                'points' => $points,
                'status'=>0,
                'createat'=>NOW_TIME,
            );   
            $Area = D('lbs_project_area');
            $result = $Area->create($AreaData);
            if ($result) {
                if (!$Area->add()) {
                    $this->error("新区域添加失败！");
                } else {
                    $this->success("新区域添加成功！", U('Area',array('mid'=>$mid,'pid'=>$pid,'aid'=>$aid)));
                }
            } else {
                $this->error($Area->getError());
            }
        } else {
            $areanamelist=M('lbs_project_areainfo')->where(array('pid'=>$pid,'status'=>1))->select();   
            $appkey=M('lbs_application')->where(array('id'=>$aid))->getField('appkey');
           
            $this->assign('_areanamelist', $areanamelist); 
            $this->assign('_appkey', $appkey); 
            $this->assign('_aid',$aid);
            $this->assign('_pid',$pid);
            $this->assign('_mid',$mid);
            $this->display();
        }
    }

    //区域编辑
    public function AreaEdit($id = "",$areainfoid='', $areaname = "", $name = "", $phone = "", $warehouse = "", $points = "",$pid='',$aid='',$mid='') {
        $areaname=M('lbs_project_areainfo')->where(array('id'=>$areainfoid))->getField("name");
        $AreaData = array('areaname' => $areaname, 'areainfoid' => $areainfoid,  'name' => $name, 'phone' => $phone, 'warehouse' => $warehouse, 'points' => $points);
        $Area = D('lbs_project_area');
        if ($Area->create($AreaData) && $Area->where(array('id' => $id))->save()) {
            $this->success('区域编辑成功！',  U('Area',array('mid'=>$mid,'pid'=>$pid,'aid'=>$aid)));
        } else {
            $this->error($Area->getError());
        }
    }

    //区域删除
    public function AreaDel($id = "") {

        IF ($id == "") {
            $this->error("区域刪除失败!");
        }
        $AreaData = array('status' => -1);
        $Area = M('lbs_project_area');
        if ($Area->where(array('id' => $id))->save($AreaData)) {
            $this->success('区域删除成功！');
        } else {
            $this->error($Area->getError());
        }
    }

    //返回一组区域
    public function AjaxAreaList($mid = "") {
        $map['mid']=$mid;
        $map['status']=array('gt',-1);
        $AreaList = D('lbs_project_area')->where($map)->select();
        $this->ajaxReturn($AreaList);
    }

    //返回一个区域
    public function AjaxArea($id) {
        $Area = D('lbs_project_area')->where(array('id' => $id))->find();
        $this->ajaxReturn($Area);
    }

    //返回一组仓库数据
    public function AjaxWarehouseList() {
        $WarehouseList = D('warehouse')->where("status=1")->select();
        $this->ajaxReturn($WarehouseList);
    }

    //判断点在那个区域
    public function CheckPoint($appkey,$pid,$lng, $lat) {
        $GeoUtils = new \Org\GrLBS\GeoUtils();
        $AID = $GeoUtils->isPointInArea($appkey,$pid,$lng, $lat);
        if ($AID['code'] > 0) {
            $Area = D('lbs_project_area')->where(array('id' => $AID['id']))->find();
            $data['info'] = "点在区域：" . $Area['areaname'];
        } else {
            $data['info'] = "点不在任何区域内";
        }
        $this->ajaxReturn($data);
    }

  
    
 
   
}
